package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConst;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import com.itheima.service.MenuServicel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/menu")
@Slf4j
public class MenuController {

    @Reference
    MenuServicel menuService;

    //新增
    @PreAuthorize("hasAuthority('CHECKITEM_ADD')")
    @RequestMapping("/add")
    public Result add(@RequestBody Menu menu){
        log.debug("MenuController:add :" +menu);
        menuService.add(menu);
        log.debug("添加菜单项成功");
        //新增成功
        return new Result(true, "添加菜单项成功");
    }

    @RequestMapping("/findByPage")
    public Result findByPage(@RequestBody QueryPageBean queryPageBean){
        log.debug("MenuController{"+queryPageBean+"}");
        PageResult pageResult = menuService.findByPage(queryPageBean);
        log.debug("菜单列表分页查询成功!!");
        return new Result(true, "菜单列表分页查询成功!!", pageResult);
    }

    //菜单编辑数据回显
    @RequestMapping("/findById")
    public Result findById(Integer id){
        log.debug("MenuController：findById:" + id);
        Menu menu = menuService.findById(id);
        log.debug("根据id查询菜单项成功!!");
        return new Result(true,"根据id查询菜单项成功!!", menu);
    }

    //菜单编辑
    @RequestMapping("/edit")
    public Result edit(@RequestBody Menu menu){
        log.debug("MenuController:edit: " + menu);
        menuService.edit(menu);
        log.debug("菜单项修改成功!!");
        return new Result(true, MessageConst.EDIT_CHECKITEM_SUCCESS);
    }

    //菜单项删除
    @RequestMapping("/delById")
    public Result delById(Integer id){
        log.debug("MenuController:delete: " + id);
        menuService.delById(id);
        log.debug("菜单项删除成功!!");
        return new Result(true, MessageConst.DELETE_CHECKITEM_SUCCESS);
    }

    //查询所有
    @RequestMapping("/findAll")
    public Result findAll(){
        log.debug("MenuController:findAll: " );
        List<CheckGroup> checkGroupList = menuService.findAll();
        log.debug("菜单组查询成功！！！");
        return new Result(true,MessageConst.QUERY_CHECKGROUP_SUCCESS,checkGroupList);
    }
}
